package com.tarena.lbs.user.service.impl;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.service.UserGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Pageable;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserGroupServiceImpl implements UserGroupService {
    private final JwtEncoder jwtEncoder;
    public UserGroupServiceImpl(JwtEncoder jwtEncoder) {
        this.jwtEncoder = jwtEncoder;
    }

    @Autowired
    private UserGroupRepository userGroupRepository;

    @Override
    public void save(UserGroupParam param,String token) throws BusinessException {
        log.debug("新增用户分组参数业务层：{}",param);
        UserGroupPO userGroupPO=new UserGroupPO();
        BeanUtils.copyProperties(param,userGroupPO);
        Object o= jwtEncoder.getLoginFromToken(token, UserPrinciple.class);
        UserPrinciple userPrinciple= (UserPrinciple) o;
        userGroupPO.setBusinessId(userPrinciple.getId());
        userGroupPO.setCreateAt(new Date());
        userGroupRepository.save(userGroupPO);
    }

    @Override
    public PageResult<UserGroupVO> pageList(UserGroupQuery userGroupQuery) {
        PageResult<UserGroupVO> pageResult = userGroupRepository.selectPageList(userGroupQuery);
        return pageResult;
    }


}
